package com.ice.app.system.log.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.util.StringUtil;
import com.ice.app.system.entity.Log;
import com.ice.app.system.mapper.LogMapper;

/**
 * 
 * 业务逻辑层：
 * 系统日志
 * 
 * @author Mr.Palo
 * @email 442803840@qq.com
 * @date 2018-12-09 21:46:57
 */
@Service
public class LogService {

	@Autowired
	private LogMapper logMapper;
	
	// ----------------------------------------自动生成--------------------------------------------
	
	/**
	 * 菜单保存
	 * 
	 * @param log
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public boolean saveLog(Log log) throws Exception {
		if (StringUtil.isNotEmpty(log.getId())) {
			// 修改操作
			Log old = this.getLog(log.getId());
			if (old != null) {
				return logMapper.updateByPrimaryKeySelective(log) > 0;
			}
			return false;
		} else {
			// 保存操作
			return logMapper.insertSelective(log) > 0;
		}
	}

	/**
	 * 根据主键删除菜单
	 * 
	 * @param key
	 * @return
	 */
	@Transactional
	public boolean deleteLog(String key) {
		return logMapper.deleteByPrimaryKey(key) > 0;
	}

	/**
	 * 根据多个主键删除菜单
	 * 
	 * @param keys
	 * @return
	 */
	@Transactional
	public boolean deleteLogs(String keys) {
		if (!StringUtils.isEmpty(keys)) {
			String[] strs = keys.split(",");
			for (String key : strs) {
				this.deleteLog(key);
			}
			return true;
		}
		return false;
	}

	/**
	 * 根据主键获取菜单实体
	 * 
	 * @param key
	 * @return
	 */
	public Log getLog(String key) {
		return logMapper.selectByPrimaryKey(key);
	}

	/**
	 * 根据给定条件获取菜单实体集合
	 * 
	 * @param key
	 * @return
	 */
	public List<Log> getLogs(Log log) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("log", log);
		return logMapper.selectByFilter(map);
	}

	// ----------------------------------------自定义（Public）---------------------------------------------
	
}